//
// Created by EDZ on 2021/10/8.
//
#include <pthread.h>
#include <iostream>
#include <vector>

#include "grammars.h"
#include "func.h"
#include "llvm/compiler.h"
#include "sockets.h"
#include "Obj.h"
#include "DbQuery.h"
#include "deelx.h"

#include "file_handlers.h"
#include "String.h"
#include "stl_algorithm.h"
#include "class_invariant.h"
#include "Vector.h"
#include "exception.h"
#include "scope.h"
#include "smart_pointers.h"
///C++20
#include "concepts.h"
#include "gramma_constexpr.h"
#include "stl_polymorphic_transform.h"
#include "stl_tuple.h"
#include "stl_tuple_iteration.h"
/// regex
#include "regex_re2.h"

void func_malloc() {
    int *b;
    int *a = (int *) malloc(sizeof(int));
    *a = 10;
    b = a;
    printf("a=%d,b=%d,&b=%p\n", *a, *b, b);
    free(a);
}

/// Google RE2 正则测试
void func_re2(int argc, char *argv[]) {
    nilyang::re2_exec_by_text(argc, argv);
}

void func_test(int argc, char *argv[]) {
    //func_compiler();
    //func_cstructs();
    //func_socket_server(argc, argv);
    //func_mysql();
    //func_gramma();
    //func_test_file();
    //func_stl_find_lt_2017();
    //func_literal_operator();
    //func_invariant();
    //func_vector_invariant();
    //func_exception();
    //func_scope();
    //func_use_pointer();
    //func_concepts_test();
    //sum_split();
    //func_grammars();
    //sum_split_teacher();
    //func_polymophic_transform();
    //func_gramma_constexpr();
    //func_compute_in_compile_time();
    func_re2(argc, argv);
    //func_call_cfunc();
}

void func_cstructs() {
    /// http://c.biancheng.net/view/2035.html
    union data {
        int n;
        char ch;
        short m;
    };

    union data a;
    printf("%lu, %lu\n", sizeof(a), sizeof(union data));
    a.n = 0x40;
    printf("%X, %c, %hX\n", a.n, a.ch, a.m);
    a.ch = '9';
    printf("%X, %c, %hX\n", a.n, a.ch, a.m);
    a.m = 0x2059;
    printf("%X, %c, %hX\n", a.n, a.ch, a.m);
    a.n = 0x3E25AD54;
    printf("%X, %c, %hX\n", a.n, a.ch, a.m);

}

void func_thread() {
    pthread_mutex_t mutex;
    pthread_cond_t cond;
    bool test_cond = false;
    /// TODO init


    /// thread 1.
    pthread_mutex_lock(&mutex);
    /// thread 2.


}

void func_socket_client(int argc, char *argv[]) {

}

void func_socket_server(int argc, char *argv[]) {
    func_sockets(argc, argv);
}

void foo_obj(int n) {
    Obj obj;
    if (n == 42) {
        throw "life, the universe and everything";
    }
}


void func_strtox() {
    char szOrbits[] = "90613.305 365.24";
    char *pEnd;
    long double f1, f2;
    f1 = strtold(szOrbits, &pEnd);
    f2 = strtold(pEnd, nullptr);
    printf("f1=%.2Lf, f2=%.2Lf.\n ", f1, f2);
    printf("Pluto takes %.2Lf years to complete an orbit.\n", f1 / f2);

}

void func_obj_exception() {
    try {
        foo_obj(41);
        foo_obj(42);
    } catch (const char *s) {
        puts(s);
    }
}

void func_vector() {
    auto ptr = new std::vector<int>();
    ptr->push_back(1);
    ptr->push_back(2);
    ptr->push_back(133);
    ptr->push_back(1433);
    for (int &cursor: *ptr) {
        std::cout << "iter=" << cursor << "\n";
    }
    for (int &p: *ptr) {
        printf("%d,", p);
    }
    for (auto cursor = ptr->begin(); cursor != ptr->end(); cursor++) {
        std::cout << "iter=" << *cursor << "\n";
    }
}

void func_compiler() {
    CompilerMainFunc();
}

void func_mysql() {
    using namespace MyDB;
    DbQuery::Exec();
}

void func_call_cfunc() {

}
